package com.shangfeng.demo.controller;

import com.shangfeng.demo.service.IIndexService;
import com.shangfeng.mvc.annotation.SFAutowired;
import com.shangfeng.mvc.annotation.SFController;
import com.shangfeng.mvc.annotation.SFRequestMapping;
import com.shangfeng.mvc.annotation.SFRequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: sfmvc
 * @description: 控制器
 * @author: weizj
 * @create: 2018-11-11 22:10
 **/
@SFController
@SFRequestMapping("index")
public class IndexController {

    @SFAutowired
    private IIndexService indexService;


    @SFRequestMapping("hello")
    public void hello(HttpServletRequest request, HttpServletResponse response, @SFRequestParam String name) {
        String result = indexService.sayHello(name);
        try {
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SFRequestMapping("age")
    public void age(HttpServletRequest request, HttpServletResponse response, @SFRequestParam Integer age) {
        String result = indexService.sayAge(age);
        try {
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @SFRequestMapping("age")
    public void nameAndAge(HttpServletRequest request, HttpServletResponse response, @SFRequestParam("name") String name, @SFRequestParam("age") Integer age) {
        String nameResult = indexService.sayHello(name);
        String ageResult = indexService.sayAge(age);
        try {
            response.getWriter().write(nameResult + ageResult);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
